﻿using System;
using System.Linq;
using System.Web.UI;
using RaceList.activity.help;

namespace RaceList.activity.mhalloween
{
    public partial class UserInfo : Page
    {
        private readonly object _obj = new object();
        public string Content { get; set; }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (IsAward())
                {
                    var awardUser = RelayQuery.GetActivityAwardUser(SessionHelp.UserData.WeixinUserId);
                    divSubmit.Visible = true;

                    string awardName = awardUser.Activity_Awards.name;

                    Content = @"<h5>获得奖品：</h5>
				                <div class='getPrize'>您获得奖品：{0}</div>
				                <h6>※请务必填写领奖信息，如不填写将视为自动放弃奖项</h6>";

                    Content = string.Format(Content, awardName);

                    txtName.Text = awardUser.name;
                    txtCode.Text = awardUser.winCode;
                    txtPhone.Text = awardUser.phone;
                }
                else
                {
                    Content = @"<h5>您还没有获得任何奖品，多玩几局中奖机率会更高哦，继续努力!</h5>";
                }
            }
        }

        public bool IsAward()
        {
            //更具awarduser来判断,更具Relay导航熟悉的weixinuserid属性来判断
            var awardUser = RelayQuery.GetActivityAwardUser(SessionHelp.UserData.WeixinUserId);
            if (awardUser != null)
            {
                return true;
            }
            return false;
        }

        protected void Submit_Click(object sender, EventArgs e)
        {
            string name = txtName.Text.Trim();
            string phone = txtPhone.Text.Trim();
            string code = txtCode.Text.Trim();

            #region 数据验证

            if (string.IsNullOrEmpty(code))
            {
                this.Alert("身份证后4位不能为空");
                txtCode.Focus();
                return;
            }
            if (string.IsNullOrEmpty(name))
            {
                this.Alert("姓名不能为空");
                txtName.Focus();
                return;
            }
            if (string.IsNullOrEmpty(phone))
            {
                this.Alert("电话不能为空");
                txtPhone.Focus();
                return;
            }
            if (!RegexHelper.IsCNMobileNum(phone))
            {
                this.Alert("电话号码格式错误");
                txtPhone.Focus();
            }

            ipowdb_suibian_webDataContext db = ConnHelper.ipowdb_suibian_web();
            var awardUser = db.Activity_AwardUser.FirstOrDefault(s => s.Activity_Relay.WeixinUserId == SessionHelp.UserData.WeixinUserId);
            awardUser.name = txtName.Text.Trim();
            awardUser.phone = txtPhone.Text.Trim();
            awardUser.winCode = txtCode.Text.Trim();
            awardUser.isAward = true;
            awardUser.AwardDate = DateTime.Now;
            db.SubmitChanges();

            this.Alert("提交成功", SessionHelp.IndexUrl);

            #endregion
        }
    }
}